package com.iris.live.services.data.repositories.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.iris.live.services.data.models.销售顾问状态Model;
import com.iris.live.services.data.repositories.ForeGroundRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
@Transactional
public class ForeGroundRepositoryImpl extends BaseRepository implements ForeGroundRepository {
	
	//获取销售未完成的接待
	@Override
	public List getUnfinishTRRecord(String permit, String salesConsultant, Date date) {
		Map params = Maps.newHashMap();
		String sql = "select `销售顾问`,`接待类型`,`开始接待时间`,`结束接待时间`,`开始试驾时间`,`结束试驾时间`,`临时接待编号`,`进店编号` "
				+ "from `临时接待记录`  where `许可`=:permit "
				+ "and `销售顾问`=:salesConsultant and `接待日期`=:date "
				+ "and `是否接待完成`='否'";
		params.put("permit", permit);
		params.put("salesConsultant", salesConsultant);
		params.put("date", date);
		return this.getList(sql, params);
	}
	
	//通过临时接待编号获取进店编号，和开始接待时间
	@Override
	public List getReceptionTimeAndNumberByRNum(Integer teampReceptionNum) {
		Map params = Maps.newHashMap();
		String sql="SELECT `进店编号`,`开始接待时间` FROM 临时接待记录 WHERE `临时接待编号`=:teampReceptionNum";
		params.put("teampReceptionNum", teampReceptionNum);
		return this.getList(sql, params);
	}

	@Override
	public List getEnterSotreNum(Integer teampReceptionNum) {
		Map params = Maps.newHashMap();
		String sql="select 进店编号 from 临时接待记录 where `临时接待编号`=:teampReceptionNum";
		params.put("teampReceptionNum", teampReceptionNum);
		return this.getList(sql, params);
	}
	
	@Override
	public List getCurrentDaySalePerson(String permit, Date date) {
        Map<String,Object> params = new HashMap<String,Object>();
		
        String sqlStr = "select `销售顾问` "
				+ "from `销售顾问状态` "
				+ "where 1=1 ";

        params.put("permit", permit);
        if (!Strings.isNullOrEmpty(permit)){
            sqlStr += " and `许可`= :permit";
            params.put("permit",permit);
        }
        
        if (date != null){
        	sqlStr += " and `日期`= :date";
            params.put("date",new SimpleDateFormat("yyyy-MM-dd").format(date));
        }
        
        return this.getList(sqlStr, params);
	}

	@Override
	public List getCurrentDaySaleWork(String permit, Date date) {
        Map<String,Object> params = new HashMap<String,Object>();
		
        String sqlStr = "select t1.`用户名`,ifnull(t2.`状态码`,'0') '状态码',"
        		+ "if(ifnull(t2.`是否当班`,'')='','0',ifnull(t2.`是否当班`,'')) '是否当班',"
        		+ "if(ifnull(t2.`排班顺序`,'999999')='','999999',ifnull(t2.`排班顺序`,'999999')) '排班顺序' "
        		+ "from ("
        			+ "select A.`用户名` '用户名' "
        			+ "from `用户` A "
        			+ "where A.`许可`=:permit AND ifnull(A.`删除状态`,'') <> '已删除' AND IFNULL(A.销售顾问职位状态,'') <> '离职' "
        				+ "AND A.`接待状态` = '是') t1 "
        			+ "LEFT JOIN ("
        				+ "SELECT `销售顾问`,`状态码`,`是否当班`,`排班顺序` "
        				+ "FROM `销售顾问状态` "
        				+ "WHERE `许可`=:permit AND `日期`=:date) t2 "
        			+ "ON t1.`用户名`=t2.`销售顾问` "
        			+ "order by ifnull(t2.`排班顺序`,'999999')";

        params.put("permit", permit);
        params.put("date",new SimpleDateFormat("yyyy-MM-dd").format(date));
        
        return this.getList(sqlStr, params);
	}

	@Override
	public List getSaleUnDoneService(String permit, Date date) {
        Map<String,Object> params = new HashMap<String,Object>();
		
        String sqlStr = "select `销售顾问`,`接待类型`,`开始接待时间`,`结束接待时间`,`开始试驾时间`,"
        		+ "`结束试驾时间`,`临时接待编号` 接待编号 "
        		+ "from `临时接待记录` "
        		+ "where `是否接待完成`='否'";
        
        params.put("permit", permit);
        if (!Strings.isNullOrEmpty(permit)){
            sqlStr += " and 许可 = :permit";
            params.put("permit",permit);
        }
        
        if (date != null){
        	sqlStr += " and 接待日期 = :date";
            params.put("date",new SimpleDateFormat("yyyy-MM-dd").format(date));
        }
        
        return this.getList(sqlStr, params);
	}

	@Override
	public List getSalePicPath(String permit) {
        Map<String,String> params = new HashMap<String,String>();
		
        String sqlStr = "SELECT 用户名,`销售顾问头像路径` "
        		+ "FROM `用户` "
        		+ "WHERE `接待状态` = '是' AND IFNULL(`删除状态`,'') <> '已删除' AND IFNULL(销售顾问职位状态,'') <> '离职' AND IFNULL(`销售顾问头像路径`,'') <> ''";
        
        params.put("permit", permit);
        if (!Strings.isNullOrEmpty(permit)){
            sqlStr += " and 许可 = :permit";
            params.put("permit",permit);
        }
        
        return this.getList(sqlStr, params);
	}

	@Override
	public String getSaleStatusCode(销售顾问状态Model saleStatusMode) {
		Map<String,Object> params = new HashMap<String,Object>();
		
		String currentDate = new SimpleDateFormat("yyyy-MM-dd").format(saleStatusMode.get日期());
		params.put("permit",saleStatusMode.get许可());
		params.put("date",currentDate);
		params.put("salePerson", saleStatusMode.get销售顾问());
		String sqlStr = "select `销售顾问` "
				+ "from `销售顾问状态` "
				+ "where `许可`= :permit and `销售顾问`=:salePerson and `日期`= :date LIMIT 1";
		
		List<Map<String,String>> sale = this.getList(sqlStr, params);
		
		if(sale != null && sale.size() > 0){
			Map<String,String> resMap = sale.get(0);
			String salePerson = (String)resMap.get("销售顾问");
			String getStaCodeSql = "";
			if("Get_Sum".equals(salePerson)){
				getStaCodeSql = "select ifnull(sum(`状态码`),0) '状态码' "
									+ "from `销售顾问状态` "
									+ "where `许可`=:permit and `日期`=:date";
			}else{
				getStaCodeSql = "select `状态码` "
									+ "from `销售顾问状态` "
									+ "where `许可`=:permit and `日期`=:date and `销售顾问`=:salePerson";
				params.put("salePerson",salePerson);
			}
			List<Map<String,String>> code = this.getList(getStaCodeSql, params);
			
			if(code != null && code.size() > 0){
				Map<String,String> statusCodeMap = code.get(0);
				return statusCodeMap.get("状态码");
			}else{
				return "-1";
			}
		}else{
			return "-1";
		}
	}

}
